// 手机
const checkPhone = (rule, value, callback) => {
  const reg = /^1[345789]\d{9}$/
  if (!reg.test(value)) {
    callback(new Error('请输入正确的手机号'))
  } else {
    callback()
  }
}
// 电话
var isTelOrFax = (rule, value, callback) => {
  if (!value) {
    callback()
  } else {
    const reg = /^(\d{3,4}-)?\d{7,8}$/
    const isTel = reg.test(value)
    if (value.length < 10 || value.length > 13 || !isTel) {
      //判断传真字符范围是在10到13
      callback(new Error('座机或传真格式如:0376-1234567'))
    } else {
      callback()
    }
  }
}
// 电话手机
var phoneOrtel = (rule, value, callback) => {
  if(!value) {
    callback()
  } else {
    const reg = /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/
    const result = reg.test(value)
    if(result) {
      callback()
    } else {
      callback(new Error('电话格式不正确'))
    }
  }
}
// 邮件
var isEmail = (rule, value, callback) => {
  if (!value) {
    callback()
  } else {
    const reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
    const email = reg.test(value)
    if (!email) {
      callback(new Error('邮箱格式如:admin@163.com'))
    } else {
      callback()
    }
  }
}
// 身份证
var isCardId = (rule, value, callback) => {
  if (!value) {
    callback()
  } else {
    const reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(([0-2][1-9])|10|20|30|31)\d{3}(\d|X|x)$/
    const card = reg.test(value)
    if (!card) {
      //判断座机为12位
      callback(new Error('身份证格式如:423024xxxx0216xxxx'))
    } else {
      callback()
    }
  }
}

var isPassword = (rule, value, callback) => {
  const reg = /^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[._~!@#$^&*])[A-Za-z0-9._~!@#$^&*]{6,11}$/
  const pass = reg.test(value)
  if (!pass) {
    callback(new Error('密码必须由大小写字母、数字、特殊符号组成,长度要在6-11位数'))
  } else {
    callback()
  }
}

var componyCode = (rule, value, callback) => {
  if (!value) {
    callback()
    return
  }
  if (!/^[A-Za-z0-9]+$/.test(value)) {
    callback('只能输入数字、字母')
  } else if (value.length && value.length != 18) {
    callback('必须为18位数字、字母')
  } else {
    callback()
  }
}
var eng = (rule, value, callback) => {
  let reg = /^[A-Za-z0-9]+$/
  const pass = reg.test(value)
  if (!pass) {
    callback(new Error('输入项必须为字母或数字'))
  } else {
    callback()
  }
}
export { checkPhone, isTelOrFax, isEmail, isCardId, isPassword, componyCode, eng, phoneOrtel }
